//
//  GameLayer.m
//  MyCrow
//
//  Created by guanhongjun on 12-7-5.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "GameLayer.h"
#import "MoreGameLayer.h"

@implementation GameLayer

-(void) goLeft {
    [[CCDirector sharedDirector] replaceScene:
     [CCTransitionPageTurn transitionWithDuration:0.5f scene:[GameLayer scene:curIndex-1] backwards:YES]];
}

-(void) goRight {
    [[CCDirector sharedDirector] replaceScene:
     [CCTransitionPageTurn transitionWithDuration:0.5f scene:[GameLayer scene:curIndex+1] backwards:NO]];
}

+(id) scene:(int)index {
    CCScene* scene = [CCScene node];
    GameLayer* layer = [[[GameLayer alloc] initWithIndex:index] autorelease];
    [scene addChild:layer];
    return scene;
}

-(GameLayer*) initWithIndex:(int)index {
    if (self = [super init]) {
        curIndex = index;
        CCLOG(@"curIndex = %d\n", curIndex);
        
        // 左箭头和右箭头
        CCSprite* leftArrow = [CCSprite spriteWithFile:@"leftArrow.png"];
        CCMenuItemImage* leftMenu = [CCMenuItemSprite itemFromNormalSprite:leftArrow selectedSprite:nil target:self selector:@selector(goLeft)];
        
        CCSprite* rightArrow = [CCSprite spriteWithFile:@"rightArrow.png"];
        CCMenuItemImage* rightMenu = [CCMenuItemSprite itemFromNormalSprite:rightArrow selectedSprite:nil target:self selector:@selector(goRight)];
        
        leftMenu.anchorPoint = ccp(0,0);
        leftMenu.position = ccp(5, 5);
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        rightMenu.anchorPoint = ccp(1, 0);
        rightMenu.position = ccp(winSize.width-5, 5);
        
        CCMenu* menu = [CCMenu menuWithItems:leftMenu, rightMenu, nil];
        menu.anchorPoint = ccp(0, 0);
        menu.position = ccp(0, 0);
        [self addChild:menu z:1];
        
        // 画面
        CCSprite* sp;
        if (index == -1) {  // 显示前封面
            sp = [CCSprite spriteWithFile:@"frontCover.png"];
            leftMenu.visible = NO;
        }
        else if (index == 0) { // 显示帮助页面
            sp = [CCSprite spriteWithFile:@"help.png"];
        }
        else if (index == GAME_PICTRUE_NUM+1) { // 显示后封面
            sp = [CCSprite spriteWithFile:@"backCover.png"];
        }
        else if (index == GAME_PICTRUE_NUM+2) { //显示广告
            [self addChild:[MoreGameLayer node]];
            rightMenu.visible = NO;
        }
        else {
            sp = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%d.png", index]];
        }
        
        // 当此页不是显示广告时
        if (index != GAME_PICTRUE_NUM+2) {
            sp.anchorPoint = ccp(0, 0);
            sp.scale = 0.5f;
            [self addChild:sp];
        }
    }
    return self;
}

@end
